/*
 * metric.h
 *
 *  Created on: 17 sept. 2013
 *      Author: the
 */

#ifndef METRIC_H_
#define METRIC_H_

#include "type.h"
#include "init.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>


//Global extern variables
extern int nbOfNodes;		//Number of nodes
extern struct node* nodes;	//Array with the nodes
extern int nbOfRadios;		//Number of radios per node
extern int maxFreq;		//Number of orthogonal frequencies
extern int algorithm;		//Algorithm number used to allocate the frequencies
extern float	L;		//Length of a side of the observation window (LxL)
extern float	R;		//Radio scope of the nodes

//Look for a given neighbor (given its ID) in the N1 list.
//Return the N1 index if found and 0 otherwise
int getIndexN1(struct node aNode, int id);

//Look for a radio number between two nodes, the struct node aNode and a neighbor with index id (the radio is set by the allocation algorithm)
int getRadioN1(struct node aNode, int id);

//Check if node id used the given frequency or not. Return -1 if not, otherwise return the index of interface using it.
int usedfreq(int id, int frequency);

//Check if node i in N1 of node id used the given frequency or not. Return 0 if not, otherwise return the index of interface using it.
int usedfreqN1(int id, int i, int frequency);

//Check if node i in N2 of node id used the given frequency or not. Return 0 if not, otherwise return the index of interface using it.
int usedfreqN2(int id, int i, int frequency);

//Return 1 if there is a route (in the routing table) from id1 to id2 (id2 is the next hop)
//return 0 otherwise
int linkIsActive(int id1, int id2);

//Return 1 if the frequency "frequency" is used between the nodes "id1" and "id2".
//Otherwise, returns 0.
int linkFrequency(int id1, int id2, int frequency);

// Verifier que le noeud id1 et id2 utilisent la fréquence 'frequency' pour communication
// Si oui, retourner 1, par contre, retourner 0
int linkUsedFrequency(int id1, int id2, int frequency);

//Return 1 if the frequency "frequency" is used between the nodes "id1" and "id2" for active route only.
//Otherwise, returns 0.
int linkFrequencyRoute(int id1, int id2, int frequency);

//Check if exist the link (not common link) between node id and it's 1-hop neiborgh with the index i, return 1 if YES
int linkexist(int id,int i);

//Check if exist the link (not common link) between node id and id2, return 1 if YES
int linkExist(int id,int id2);

//Check if exist the link (all the frequency) between node id and id2, return 1 if YES
int linkExist2(int id,int id2);

//Chek if node "id" has a free radio. Returns -1 if no.
//Returns the number of the first radio available if it exists.
int freeRadio(int id);

//Compute the conflict graph for a link.
int conflictGraphLink(int id1, int id2, int frequency);

//Return the cardinal of the conflict graph in the 2-neighborhood for a given frequency.
//Only information of nodes[id] must take into account.
int conflictGraphN2(int id, int frequency);

//Return the cardinal of the conflict graph in the whole network.
//The argument is the first frequency we consider (=1 when we do not consider the common frequency -> frequency 0)
//it is 1 when we want the conflict graph for all teh frequency.
int conflictGraph(int freqStart);
int conflictGraphGreedy(int freqStart);

//Return the cardinal of the conflict graph in the whole network.
//We does count the conflict graph only on the common frequency (frequency 0)
int conflictGraphCommonChannel();

//Update a temporal and technical table used by the updateRoutingTableDijsktra() and updateRouteSourceDestination() functions.
//It is in fact teh table allowing us to compute the best path.
//The malloc/free for this table must be done in the call function.
void updateTableEntry(struct tableEntry *table, int nodeId);


//Compute the routing table of node id.
//We use a modified version of the Dijkstra algorithm. The cost is the min of the metric on the path (field in the info-neighbor structure).
//When two paths have the same costs, we choose teh one with the smallest number of hops.
void updateRoutingTableDijsktra(int id);


//Compute the routing table of node id but for a specific destination.
//We use a modified version of the Dijkstra algorithm. The cost is the min of the metric on the path (field in the info-neighbor structure).
//When two paths have the same costs, we choose the one with the smallest number of hops.
//routeNumber is the route number (remember that we may have several routes).
int updateRouteSourceDestination(int source, int destination, int routeNumber);


#endif /* METRIC_H_ */
